package com.bosch.in.rtp1.ept.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.bosch.in.rtp1.ept.action.form.FrozenColumnForm;
import com.bosch.in.rtp1.ept.constant.FrozenTableColumnName;
import com.bosch.in.rtp1.ept.constant.TableType;
import com.bosch.in.rtp1.ept.dao.FrozenColumnDao;
import com.bosch.in.rtp1.ept.model.dto.ScrollingColumn;
import com.bosch.in.rtp1.ept.util.SQLStatementLoader;

/**
 * Concrete JDBC implementation of FrozenColumnDao. 
 * Provide actions for updating/loading on Frozen Column Configuration table.
 * @author TPR1HC
 *
 */
public class FrozenColumnJDBCDao extends GenericDao implements FrozenColumnDao{

	private static final Logger LOGGER = Logger.getLogger(FrozenColumnJDBCDao.class);
	
	/**
	 * Constructor to inject connection
	 * @param con	DB connection
	 */
	public FrozenColumnJDBCDao(Connection connection) {
		super(connection);
	}

	@Override
	public boolean clearStartScrollingColumn() throws SQLException {
		LOGGER.debug("Begin clearStartScrollingColumn");
		try {
			String updateStm = SQLStatementLoader.getFrzColClear();
			return executeUpdate(updateStm,null);
		} finally {
			LOGGER.debug("End clearStartScrollingColumn");
		}
	}

	@Override
	public boolean setStartScrollingColumn(List<ScrollingColumn> frozenColumns) throws SQLException {
		LOGGER.debug("Begin setStartScrollingColumn");
		try {
			if (frozenColumns.size() == 0) {
				// do nothing
				return true;
			}
			
			String updateStm = SQLStatementLoader.getFrzColUpdate();
			
			// set parameter values for updated frozen columns
			Object[][] params = new Object[frozenColumns.size()][];
			for (int i = 0; i < frozenColumns.size(); i++) {
				params[i] = new Object[2];
				params[i][0] = frozenColumns.get(i).getFrozenColumn();
				params[i][1] = frozenColumns.get(i).getTableType().toInt();
			}
			
			return executeBatchUpdate(updateStm, params);
		} finally {
			LOGGER.debug("End setStartScrollingColumn");
		}
	}

	@Override
	public FrozenColumnForm getFrozenColumnForm() throws SQLException {
		LOGGER.debug("Begin getFrozenColumnForm");
		
		FrozenColumnForm fzColForm = new FrozenColumnForm();
		
		String queryString = SQLStatementLoader.getAllColsSelectQuery();
		ResultSet resultSet = executeQuery(queryString,null);
		while (resultSet.next()) {
			int scroll = resultSet.getInt(FrozenTableColumnName.SCROLL_FLAG);
			int tableId = resultSet.getInt(FrozenTableColumnName.TABLE_ID);
			String columnName = resultSet.getString(FrozenTableColumnName.COL_NAME);
			if (tableId == TableType.LOT_STATUS.toInt()) {
				fzColForm.getLotStatusCols().add(columnName);
				
				if (scroll == 1) {
					fzColForm.setLotStatusStartScrollingCol(columnName);
				}
				
			} else {
				fzColForm.getReleasedHoldCols().add(columnName);
				
				if (scroll == 1) {
					fzColForm.setReleasedHoldStartScrollingCol(columnName);
				}
			}
		}
		// close statement, result set
		resultSet.close();
		close();
		LOGGER.debug("End getFrozenColumnForm");
		
		return fzColForm;
	}

	

}
